import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/components/ad_banner/insert_ad.dart';
import 'package:pornhub_app/components/circle_image.dart';
import 'package:pornhub_app/utils/enum.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/player/controllers/video_play_controller.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/color.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/views/player/views/video_progress_download_button.dart';
import 'package:pornhub_app/views/videotag/tag_videos.dart';
import '../../../utils/utils.dart';

class PlayerCommonInfoWidget extends StatelessWidget {
  final VideoPlayController vc;
  const PlayerCommonInfoWidget({super.key, required this.vc});

  _buildVideoTitle() {
    return Text(
      vc.currentVideo.title ?? '',
      maxLines: 2,
      textAlign: TextAlign.left,
      style: TextStyle(
        color: COLOR.hexColor("#fff"),
        fontSize: 15.w,
        fontWeight: FontWeight.w500,
        overflow: TextOverflow.ellipsis,
      ),
    ).paddingOnly(top: 13.w, left: 14.w, right: 14.w);
  }

  _buildType() {
    var text = vc.currentVideo.videoType == 1
        ? 'VIP'
        : vc.currentVideo.videoType == 2
            ? '${vc.currentVideo.price!.truncate()}金币'
            : '';
    if (text.isEmpty) return const SizedBox.shrink();
    return Container(
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2.w),
          border: Border.all(color: COLOR.hexColor('#FEE041'))),
      padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 2.w),
      child: Text(text,
          style: kTextStyle(COLOR.hexColor("#FEE041"), fontsize: 12)),
    );
  }

  _buildTags() {
    final tags = vc.currentVideo.tags ?? [];
    final mark = 1;
    return tags.isEmpty
        ? const SizedBox.shrink()
        : Wrap(
            spacing: 10.w,
            runSpacing: 10.w,
            children: [
              _buildType(),
              ...tags.map(
                (e) => Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(2.w),
                      border: Border.all(color: COLOR.hexColor('#ACABB1'))),
                  padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 2.w),
                  child: Text(e.tagsTitle,
                      style:
                          kTextStyle(COLOR.hexColor("#CCCBCE"), fontsize: 12)),
                ).onTap(() {
                  Navigator.push(
                    Get.context!,
                    MaterialPageRoute(
                      builder: (BuildContext context) {
                        return TagVideos(
                            mark: mark, tagsTitle: e.tagsTitle, videoMark: 1);
                      },
                    ),
                  );
                }),
              )
            ],
          ).marginOnly(top: 10.w, left: 14.w, right: 14.w);
  }

  _buildLogo() {
    return vc.currentVideo.videoId == null
        ? const SizedBox.shrink()
        : Container(
            padding: EdgeInsets.symmetric(horizontal: 14.w),
            height: 40.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                CircleImage.network(
                  vc.currentVideo.logo!,
                  size: 40.w,
                  onTap: () {
                    // Get.toBloggerDetail(userId: vc.currentVideo.userId!);
                  },
                ),
                Expanded(
                    child: SizedBox(
                  height: 40.w,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(vc.currentVideo.nickName!,
                          style: kTextStyle(COLOR.hexColor("#fff"),
                              fontsize: 14.w, weight: FontWeight.w600)),
                      Text(
                          "${Utils.numFmtCh(vc.currentVideo.fakeWatchNum!)}次播放  ${Utils.dateFmt(vc.currentVideo.createdAt!)}发布",
                          style: kTextStyle(COLOR.hexColor("#A4A4B2"),
                              fontsize: 9.w)),
                    ],
                  ),
                ).paddingOnly(left: 12.w)),
                SizedBox(
                        width: 68.w,
                        height: 26.w,
                        child: Image.asset(vc.currentVideo.attention == true
                            ? 'assets/player/focus_y.png'
                            : 'assets/player/focus.png'))
                    .onTap(() {
                  vc.focusAction();
                })
              ],
            ),
          ).marginOnly(top: 15.w);
  }

  _buildOneBtn(
    String title,
    String assPath, {
    VoidCallback? tap,
  }) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Image.asset(
          assPath,
          width: 20.w,
        ),
        SizedBox(width: 5.w),
        Text(
          title,
          style: kTextStyle(COLOR.hexColor("#fff"), fontsize: 12.w),
        )
      ],
    ).onTap(tap?.call);
  }

  _buildBtns() {
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          VideoProgressDownloadButton(
            detail: vc.currentVideo,
            vc: vc,
          ),
          _buildOneBtn("分享领VIP", "share", tap: () {
            Get.toShare();
          })
        ],
      ).marginOnly(top: 15.w, left: 30.w, right: 30.w),
    );
  }

  _buildAds() {
    return InsertAd(
      adress: AdPlaceNameEnum.PLAY_MARK,
      height: 120.w,
      margin: EdgeInsets.only(left: 14.w),
    );
  }

  _buildLine() {
    return Container(
      height: 1.w,
      color: COLOR.hexColor("#292A31"),
    ).paddingOnly(top: 10.w);
  }

  // _buildRelatedWorkItems() {
  //   if (vc.representWorks.isEmpty) {
  //     return const SizedBox.shrink();
  //   }
  //   final isdm =
  //       false; //vc.currentVideo.mark != null && vc.currentVideo.mark == 2;
  //   return Column(
  //     crossAxisAlignment: CrossAxisAlignment.start,
  //     children: [
  //       Row(
  //         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //         crossAxisAlignment: CrossAxisAlignment.center,
  //         children: [
  //           Text(
  //             isdm ? '相关剧集' : "相关作品",
  //             textAlign: TextAlign.left,
  //             style: kTextStyle(COLOR.hexColor("#333"),
  //                 fontsize: 16.w, weight: FontWeight.w600),
  //           ),
  //           isdm
  //               ? Text("${vc.representWorks.length}部影片",
  //                   style: kTextStyle(COLOR.color_999999, fontsize: 11.w))
  //               : Icon(
  //                   Icons.arrow_forward_ios_outlined,
  //                   size: 15.w,
  //                   color: COLOR.hexColor("#999"),
  //                 )
  //         ],
  //       ).onTap(() {
  //         if (isdm) {
  //           return;
  //         }
  //         // Get.toBloggerDetail(userId: vc.currentVideo.userId!);
  //       }),
  //       SizedBox(height: 10.w),
  //       SizedBox(
  //         height: 145.w,
  //         child: GridView.builder(
  //           itemCount: vc.representWorks.length,
  //           scrollDirection: Axis.horizontal,
  //           gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  //               crossAxisCount: 1,
  //               mainAxisSpacing: 8.w,
  //               crossAxisSpacing: 0,
  //               childAspectRatio: 145 / 182),
  //           itemBuilder: (context, index) {
  //             final item = vc.representWorks[index];
  //             return RelatecVideoCell(
  //               model: item,
  //               width: 182.w,
  //               height: 145.w,
  //             ).onTap(() {
  //               vc.getVideoById(item.videoId!);
  //             });
  //           },
  //         ),
  //       ),
  //     ],
  //   ).marginOnly(top: 14.w, bottom: 15.w);
  // }

  _buildTips() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.symmetric(vertical: 10.w),
      child: Text.rich(TextSpan(children: [
        TextSpan(
            text: '*部分地区若遇视频无法播放，请尝试切换',
            style: TextStyle(color: Colors.white, fontSize: 12.w)),
        TextSpan(
            text: '移动网络或开VPN',
            style: TextStyle(color: COLOR.hexColor('#B93FFF'), fontSize: 12.w)),
      ])),
    );
  }

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildLogo(),
          _buildVideoTitle(),
          _buildTags(),
          SizedBox(height: 30.w),
          _buildBtns(),
          SizedBox(height: 18.w),
          _buildLine(),
          _buildTips(),
          _buildAds(),
        ],
      ),
    );
  }
}
